/*
 *	CHANGE LOG
 * 
 * 01.02.09
 * 
 *			12/11/2008 - DW - Added localization for the restoreText property of the image expander viewer.
 * 
 * 01.03.03
 * 
 *			01/20/2009 - DW - Changed the calls to GetString to take a language parameter.  The current implementation
 *								simply uses the DefaultLanguage property from the PortalSettings object which is retrieved
 *								from the HttpContext.
 * 
 */ 


using System;
using System.Web;
using DotNetNuke.Entities.Portals;

namespace ITCrossing
{
	/// <summary>
	/// Summary description for $codebehindclassname$
	/// </summary>
	public class MetaPostExpander : IHttpHandler
	{

		public void ProcessRequest(HttpContext context)
		{
			string scriptLoaders;
			bool containsTabs = (context.Request["tabs"] != null);
			bool containsHsContent = (context.Request["hs"] == null || (context.Request["hs"] != null && context.Request["hs"].ToString() == "1"));
			bool containsVideo = (context.Request["vid"] != null);
			context.Response.ContentType = "application/x-javascript";
			context.Response.Cache.SetExpires(DateTime.Now.AddYears(1));
			context.Response.Cache.SetCacheability(HttpCacheability.Public);
			//context.Response.ContentEncoding = System.Text.Encoding.UTF8;

			string appPath = MetaPostServices.GetAppPath();

			scriptLoaders = GetScriptLoaders(appPath);

				context.Response.Write(scriptLoaders 
					+ HsScriptAndStyleLoaders(containsHsContent, appPath) 
					+ TabScriptAndStyleLoaders(containsTabs, appPath)
					+ VideoScriptLoader(containsVideo, appPath));
		}

		private string GetScriptLoaders(string appPath)
		{
			string scriptLoaders;
			scriptLoaders = "function itc_addScript(scriptUrl,key){\n"
			                + "if (!document[key]){\n"
			                + "var itc_head = document.getElementsByTagName('head')[0];\n"
			                + "var itc_script = document.createElement('script');\n"
			                + "itc_script.type = 'text/javascript';\n"
			                + "itc_script.src = '" + appPath + "' + scriptUrl;\n"
			                + "itc_head.appendChild(itc_script);\n"
			                + "document[key] = true;\n"
			                + "}}\n"
			                + "function itc_addLink(linkUrl,key){\n"
			                + "if (!document[key]) {\n"
			                + "var hd = document.getElementsByTagName('head')[0];\n"
			                + "var st = document.createElement('link');\n"
			                + "st.type = 'text/css';\n"
			                + "st.rel = 'stylesheet';\n"
							+ "st.href = '" + appPath + "' + linkUrl;\n"
			                + "hd.insertBefore(st,hd.firstChild); \n"
			                + "document[key] = true;\n"
			                + "}}\n"
			                + "function itc_addStyle(newStyle){\n"
			                + "if (!document.itc_style) {\n"
			                + "var hd = document.getElementsByTagName('head')[0];\n"
			                + "var st = document.createElement('style');\n"
			                + "st.type = 'text/css';\n"
			                +
			                "if(st.styleSheet)st.styleSheet.cssText = newStyle; else st.appendChild(document.createTextNode(newStyle));\n"
			                + "hd.insertBefore(st,hd.firstChild); \n"
			                + "document.itc_style = true;\n"
			                + "}}\n";
			return scriptLoaders;
		}

		private string HsScriptAndStyleLoaders(bool containsHsContent, string appPath)
		{
			string script = string.Empty;

			if (containsHsContent)
			{
				PortalSettings portalSettings = HttpContext.Current.Items["PortalSettings"] as PortalSettings;
				// If for some reason the PortalSettings object isn't stored in the HTTP Context object, we'll just
				// assume that it's en-US
				string language = (portalSettings == null) ? "en-US" : portalSettings.DefaultLanguage;

				script = "function a_itchsLoad() {if (!document.itc_exp_loaded) {var o = window.onload;if (typeof window.onload != 'function') {window.onload = a_itc_hs;} else {window.onload = function() {o();a_itc_hs();}}document.itc_exp_loaded = true;}}\n"
				         + "function a_itc_hs() {mp.graphicsDir = '" + appPath +
				         "/DesktopModules/itcMetaPost/js/itcexpand/graphics/';"
				         + "mp.captionEval = 'this.thumb.title';"
				         + "mp.showCredits = false;"
				         + "mp.allowWidthReduction = true;"
				         + "mp.moveText = '" + MetaPostServices.GetStringGlobal("Move", "Move", language) + "';"
						 + "mp.closeText = '" + MetaPostServices.GetStringGlobal("Close", "Close", language) + "';"
						 + "mp.loadingText = '" + MetaPostServices.GetStringGlobal("Loading", "Loading", language) + "';"
						 + "mp.restoreTitle = '" + MetaPostServices.GetStringGlobal("RestoreViewer", "Click to close image, click and drag to move. Use arrow keys for next and previous.", language) + "';"
				         + "var itc_a = document.getElementsByTagName('a');"
				         +
				         "for (var i = 0; i < itc_a.length; i++) {if ((itc_a[i].className && itc_a[i].className == 'itcexpand')) {void(itc_a[i].onclick = function() { return mp.expand(this) });}}"
				         + "}\n"
				         + "a_itchsLoad();\n"
				         +
				         "itc_addScript('/DesktopModules/itcMetaPost/js/itcexpand/itcexpand.js','itc_script');\n"
				         + "if (!document.itc_style) {var itcS3 = ''\n"
				         + "+ '.itcexpand, .itcexpando {'\n"
				         + "+ '		cursor: url(" + appPath +
				         "/DesktopModules/itcMetaPost/js/itcexpand/graphics/zoomin.cur), pointer;'\n"
				         + "+ '		outline: none;'\n"
				         + "+ '}'\n"
				         + "+ '.itcexpand-active-anchor img {'\n"
				         + "+ '		visibility: hidden;'\n"
				         + "+ '}'\n"
				         + "+ '.itcexpand img {'\n"
				         + "+ '		border: 2px solid gray;'\n"
				         + "+ '}'\n"
				         + "+ '.itcexpand:hover img {'\n"
				         + "+ '		border: 2px solid white;'\n"
				         + "+ '}.itcexpand-wrapper {'\n"
				         + "+ '		background: white;'\n"
				         + "+ '}'\n"
				         + "+ '.itcexpand-image {'\n"
				         + "+ '		border: 2px solid white;'\n"
				         + "+ '}'\n"
				         + "+ '.itcexpand-image-blur {'\n"
				         + "+ '}'\n"
				         + "+ '.itcexpand-html {'\n"
				         + "+ '    background-color: white;'\n"
				         + "+ '}'\n"
				         + "+ '.itcexpand-html-blur {'\n"
				         + "+ '}'\n"
				         + "+ '.itcexpand-html-content {'\n"
				         + "+ '	position: absolute;'\n"
				         + "+ '    display: none;'\n"
				         + "+ '}'\n"
				         + "+ '.itcexpand-caption {'\n"
				         + "+ '		display: none;'\n"
				         + "+ '		border: 2px solid white;'\n"
				         + "+ '		border-top: none;'\n"
				         + "+ '		font-family: Verdana, Helvetica;'\n"
				         + "+ '		font-size: 10pt;'\n"
				         + "+ '		padding: 5px;'\n"
				         + "+ '		background-color: white;'\n"
				         + "+ '}'\n"
				         + "+ '.itcexpand-loading {'\n"
				         + "+ '		display: block;'\n"
				         + "+ '		color: black;'\n"
				         + "+ '		font-size: 8pt;'\n"
				         + "+ '		font-family: sans-serif;'\n"
				         + "+ '		font-weight: bold;'\n"
				         + "+ '		text-decoration: none;'\n"
				         + "+ '		padding: 2px;'\n"
				         + "+ '		border: 1px solid black;'\n"
				         + "+ '		background-color: white;'\n"
				         + "+ '		padding-left: 22px;'\n"
				         + "+ '		background-image: url(" + appPath +
				         "/DesktopModules/itcMetaPost/js/itcexpand/graphics/loader.white.gif);'\n"
				         + "+ '		background-repeat: no-repeat;'\n"
				         + "+ '		background-position: 3px 1px;'\n"
				         + "+ '}'\n"
				         + "+ '/* Styles for the popup */'\n"
				         + "+ '.itcexpand-wrapper {'\n"
				         + "+ '		background-color: white;'\n"
				         + "+ '}'\n"
				         + "+ '.itcexpand-wrapper .itcexpand-html-content {'\n"
				         + "+ '		width: 400px;'\n"
				         + "+ '		padding: 5px;'\n"
				         + "+ '}'\n"
				         + "+ '.itcexpand-wrapper .itcexpand-header div {'\n"
				         + "+ '}'\n"
				         + "+ '.itcexpand-wrapper .itcexpand-header ul {'\n"
				         + "+ '		margin: 0;'\n"
				         + "+ '		padding: 0;'\n"
				         + "+ '		text-align: right;'\n"
				         + "+ '}'\n"
				         + "+ '.itcexpand-wrapper .itcexpand-header ul li {'\n"
				         + "+ '		display: inline;'\n"
				         + "+ '		padding-left: 1em;'\n"
				         + "+ '}'\n"
				         +
				         "+ '.itcexpand-wrapper .itcexpand-header ul li.itcexpand-previous, .itcexpand-wrapper .itcexpand-header ul li.itcexpand-next {'\n"
				         + "+ '		display: none;'\n"
				         + "+ '}'\n"
				         + "+ '.itcexpand-wrapper .itcexpand-header a {'\n"
				         + "+ '		font-weight: bold;'\n"
				         + "+ '		color: gray;'\n"
				         + "+ '		/*text-transform: uppercase;*/'\n"
				         + "+ '		text-decoration: none;'\n"
				         + "+ '}'\n"
				         + "+ '.itcexpand-wrapper .itcexpand-header a:hover {'\n"
				         + "+ '		color: black;'\n"
				         + "+ '}'\n"
				         + "+ '.itcexpand-wrapper .itcexpand-header .itcexpand-move a {'\n"
				         + "+ '		cursor: move;'\n"
				         + "+ '}'\n"
				         + "+ '.itcexpand-wrapper .itcexpand-footer {'\n"
				         + "+ '		height: 11px;'\n"
				         + "+ '}'\n"
				         + "+ '.itcexpand-wrapper .itcexpand-footer .itcexpand-resize {'\n"
				         + "+ '		float: right;'\n"
				         + "+ '		height: 11px;'\n"
				         + "+ '		width: 11px;'\n"
				         + "+ '		background: url(" + appPath +
				         "/DesktopModules/itcMetaPost/js/itcexpand/graphics/resize.gif);'\n"
				         + "+ '}'\n"
				         +
				         "+ '.itcexpand-wrapper .itcexpand-body {text-align:left;padding:10px 10px 0px 10px;'\n"
				         + "+ '}'\n"
				         + "+ '.itcexpand-move {'\n"
				         + "+ '		cursor: move;'\n"
				         + "+ '}.itcexpand-overlay {'\n"
				         + "+ '		display: none;'\n"
				         + "+ '}a.itcexpand-full-expand {'\n"
				         + "+ '		background: url(" + appPath +
				         "/DesktopModules/itcMetaPost/js/itcexpand/graphics/fullexpand.gif) no-repeat;'\n"
				         + "+ '		display: block;'\n"
				         + "+ '		margin: 0 10px 10px 0;'\n"
				         + "+ '		width: 34px;'\n"
				         + "+ '		height: 34px;'\n"
				         + "+ '}'\n"
				         + "+ '/* Necessary for functionality */'\n"
				         + "+ '.itcexpand-display-block {'\n"
				         + "+ '		display: block;'\n"
				         + "+ '}'\n"
				         + "+ '.itcexpand-display-none {'\n"
				         + "+ '		display: none;'\n"
				         + "+ '}'\n"
				         + "+ '.itcexpand-super {display: none;} '\n"
				         + "+ '@media print {'\n"
				         + "+ '		.itcexpand-html-content {display: block; position:relative;} '\n"
				         + "+ '		.itcexpand-header,.itcexpand-footer {display:none;} '\n"
				         + "+ '		.itcexpand-super {display:inline;} '\n"
				         + "+ '}'\n"
				         + "+ ''};itc_addStyle(itcS3);\n";
			}

			return script;
		}

		public bool IsReusable
		{
			get
			{
				return true;
			}
		}
		
		private string TabScriptAndStyleLoaders (bool containsTabs, string appPath)
		{
			string script = string.Empty;

			if (containsTabs)
			{
				script =
					"itc_addLink('/DesktopModules/itcMetaPost/js/yuitabs/tabview.css','itc_tabs_tabview');\n"
					+ "itc_addScript('/DesktopModules/itcMetaPost/js/yuitabs/yuitabs.js','itc_yui_tabs');\n";
					//+ "itc_addScript('/DesktopModules/itcMetaPost/js/yuitabs/element-beta-min.js','itc_tabs_elementbeta');\n"
					//+ "itc_addScript('/DesktopModules/itcMetaPost/js/yuitabs/tabview-min.js','itc_tabs_tabviewmin');\n";
			}

			return script;
		}
		private string VideoScriptLoader (bool containsVideo, string appPath)
		{
			string script = string.Empty;

			if (containsVideo)
			{
				// Removed since the SWFObject script is added above each video.
				// This decision was made since the SWFOjbect script file hadn't loaded before the 
				// video script was being called in the page.
				//script =
				//	"itc_addScript('/Desktopmodules/itcMetaPost/js/itcexpand/swfobject.js','itc_swfobject');\n";
			}
			return script;
		}
	}
}
